/**
 * DateUtil.java
 * 
 * Create Version: 1.0
 * Author: lilang
 * Create Date: 2012-9-27
 * 
 * Copyright (c) 2012 CQCIS. All Right Reserved.
 */
package oa.base.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * DateUtil 时间日期工具
 * 
 * @author lilang
 */
public class DateUtil {
	/**
	 * 默认时间格式
	 */
	public static final String DEFAULT_FORMAT = "yyyy-MM-dd HH:mm:ss";

	public static final String YEAR_MONTH_DAY = "yyyy-MM-dd";
	
	public static final String YEAR_MONTH_DAY_LH = "yyyyMMdd";
	
	public static final String DATE_FORMATyyyyMMdd_CHN = "yyyy年MM月dd日";

    public final static String DATE_FORMAT_yyyyMMddHHmmssSSS = "yyyyMMddHHmmssSSS";
    
    public final static String DATE_FORMAT_yyyyMMddHHmmss = "yyyyMMddHHmmss";
	
	public static final long DAY_MOLIS = 1000 * 60 * 60 * 24;
	public static final long HOUR_MOLIS = 1000 * 60 * 60;
	public static final long MINUTE_MOLIS = 1000 * 60;
	public static final long SECOND_MOLIS = 1000;

	/**
	 * 获取当前默认格式的时间 yyyy-MM-dd HH:mm:ss
	 * 
	 * @return
	 */
	public static String getCurrentTime() {
		Date time = new Date();
		SimpleDateFormat format = new SimpleDateFormat(DEFAULT_FORMAT);
		return format.format(time);
	}

	public static synchronized long getSysTimeGenerator() {
		Date date = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat(DATE_FORMAT_yyyyMMddHHmmssSSS);
		return Long.valueOf(formatter.format(date));
	}

	/**
	 * 解析时间
	 * 
	 * @param time
	 * @param format
	 * @return
	 * @throws ParseException
	 */
	public static Date parseDate(String time, String format) throws ParseException {
		SimpleDateFormat formatTime = new SimpleDateFormat(format);
		return formatTime.parse(time);
	}

	/**
	 * 当前时间的数据以你传入的格式返回
	 * 
	 * @return
	 */
	public static String getCurrentTime(String formatString) {
		Date time = new Date();
		SimpleDateFormat format = new SimpleDateFormat(formatString);
		return format.format(time);
	}

	/**
	 * 当前时间的数据以你传入的格式返回
	 * 
	 * @return
	 */
	public static String getCurrentTime(Date date, String formatString) {
		SimpleDateFormat format = new SimpleDateFormat(formatString);
		return format.format(date);
	}

	/**
	 * 获取当年
	 * 
	 * @return
	 */
	public static int getYear() {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		return calendar.get(Calendar.YEAR);
	}
	
	/**
	 * 获取当年
	 * 
	 * @return
	 */
	public static int getYear(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.YEAR);
	}

	/**
	 * 年份加减
	 * 
	 * @return
	 */
	public static int addYear(Date date, int num) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.YEAR, num);
		return calendar.get(Calendar.YEAR);
	}

	/**
	 * 获取当月
	 * 
	 * @return
	 */
	public static int getMonth() {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		return calendar.get(Calendar.MONTH) + 1;
	}
	
	/**
	 * 获取当月
	 * 
	 * @return
	 */
	public static int getMonth(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.MONTH) + 1;
	}

	/**
	 * 月份加减
	 * 
	 * @return
	 */
	public static int addMonth(Date date, int num) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MONTH, num);
		return calendar.get(Calendar.MONTH) + 1;
	}

	/**
	 * 获得当月的最后一天
	 * 
	 * @return
	 */
	public static int lastMonth(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.getMaximum(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 获取时间最后一个月的时间
	 * 
	 * @param date
	 * @return
	 */
	public static Date getMonthLastDay(Date date) {
		Calendar calendar = Calendar.getInstance();
		if (null != date) {
			calendar.setTime(date);
		}
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DATE));
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		calendar.set(Calendar.MILLISECOND, 999);
		return calendar.getTime();
	}

	/**
	 * 获取当天
	 * 
	 * @return
	 */
	public static int getDay() {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		return calendar.get(Calendar.DATE);
	}

	/**
	 * 天数加减
	 * 
	 * @return
	 */
	public static int addDay(Date date, int num) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DATE, num);
		return calendar.get(Calendar.DATE);
	}
	
	/**
	 * 时间
	 * 
	 * @return
	 */
	public static Date addHour(Date date, int hour) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.HOUR, hour);
		return calendar.getTime();
	}

	/**
	 * 当前小时
	 * 
	 * @return
	 */
	public static int getHour() {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		return calendar.get(Calendar.HOUR_OF_DAY);
	}

	/**
	 * 当前分
	 * 
	 * @return
	 */
	public static int getMinute() {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		return calendar.get(Calendar.MINUTE);
	}

	/**
	 * 当前秒
	 * 
	 * @return
	 */
	public static int getSecond() {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		return calendar.get(Calendar.SECOND);
	}

	/**
	 * 比较2个时间的大小 第一个大于第二个 返回 true
	 * 
	 * @param frist
	 * @param two
	 * @return
	 */
	public static boolean compareTime(Date frist, Date two) {
		Calendar oneTime = Calendar.getInstance();
		Calendar twoTime = Calendar.getInstance();
		oneTime.setTime(frist);
		twoTime.setTime(two);
		return oneTime.after(twoTime);
	}

	/**
	 * 显示2个时间差 到天数
	 * 
	 * @param frist
	 * @param two
	 * @return
	 */
	public static String timeDifference(Date frist, Date two) {
		long timeDifference = frist.getTime() - two.getTime();
		boolean isTrue = false;
		if (timeDifference >= 0) {
			isTrue = true;
		}
		// 绝对值处理
		timeDifference = Math.abs(timeDifference);
		long day = timeDifference / DAY_MOLIS;
		long hour = timeDifference % DAY_MOLIS / HOUR_MOLIS;
		long minute = timeDifference % HOUR_MOLIS / MINUTE_MOLIS;
		long second = timeDifference % MINUTE_MOLIS / SECOND_MOLIS;
		if (isTrue) {
			return "两个时间相差：" + day + "天" + hour + "小时" + minute + "分" + second + "秒";
		} else {
			return "两个时间相差为负的：" + day + "天" + hour + "小时" + minute + "分" + second + "秒";
		}

	}
	/**
	 * 获取星期
	 * @return
	 * lilang 
	 * 2013-10-15下午2:41:42
	 */
	public static String getWeek(Date date){
		Calendar oneTime = Calendar.getInstance();
		oneTime.setTime(date);
		String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
         int w = oneTime.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];
	}
	/**
     * 取得显示时间
     * @return
     */
    public static String showWeekTime(Date date){
    	String time = getCurrentTime(date,DATE_FORMATyyyyMMdd_CHN);
    	return time+" "+getWeek(date);
    }
	
	
	
	public static void main(String[] args) throws ParseException {
		System.out.println(DateUtil.timeDifference(new Date(),
				DateUtil.parseDate("2011-02-11 11:11:11", DateUtil.DEFAULT_FORMAT)));
		// System.out.println(DateUtil.compareTime(new Date(), DateUtil.parseDate("2013-02-11 11:11:11",
		// DateUtil.DEFAULT_FORMAT)));
		// System.out.println(DateUtil.addDay(new Date(), -29));
	}
}
